package com.zk.babala.datasource;

import com.zk.babala.entity.CategoryInfo;
import com.zk.babala.entity.LineInfo;
import com.zk.babala.entity.Movie;
import com.zk.babala.entity.PlayNumInfo;
import com.zk.babala.entity.VideoInfo;
import com.zk.babala.service.ISourceParse;
import com.zk.babala.util.LineUtil;
import com.zk.babala.util.RequestUtil;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 红果短剧
 */
public class HGNetSource implements ISourceParse {
    private String nextUrl;

    @Override
    public Map<String, String> getSourceMeta() {
        Map<String, String> map = new HashMap<>();
        map.put("source", this.getClass().getSimpleName());
        map.put("name", "红果net");
        map.put("url", "https://www.hguo.net/");
        return map;
    }

    public HGNetSource() {
    }

    @Override
    public List<CategoryInfo> getCategory() {
        String line = LineUtil.getLineUtil().getLine();
        List<CategoryInfo> categoryInfoList = new ArrayList<>();
        categoryInfoList.add(new CategoryInfo("每日推荐", line));
        categoryInfoList.add(new CategoryInfo("短剧", line + "vodtype/5.html"));
        categoryInfoList.add(new CategoryInfo("电影", line + "vodtype/1.html"));
        categoryInfoList.add(new CategoryInfo("电视剧", line + "vodtype/2.html"));
        categoryInfoList.add(new CategoryInfo("综艺", line + "vodtype/4.html"));
        return categoryInfoList;
    }

    String nextPageClass = "MorePagination_linkItem";
    String moviesItemClass;
    String moviesLinkClass;

    @Override
    public List<Movie> getMovies(String url) {
        if (url.equals(LineUtil.getLineUtil().getLine())) {
            moviesItemClass = "FeaturedList_featuredItem";
            moviesLinkClass = "FeaturedList_bookName";
        } else if (url.contains(getSearchUrl())) {
            moviesItemClass = "MTagBookList_tagBookItem";
            moviesLinkClass = "MTagBookList_bookName";
        } else {
            moviesItemClass = "BrowseList_listItem";
            moviesLinkClass = "BrowseList_bookName";
        }
        List<Movie> list = new ArrayList<>();
        Document document = RequestUtil.get(url);
        document.getElementsByClass(moviesItemClass).forEach(item -> {
            Element itemA = item.getElementsByClass(moviesLinkClass).get(0);
            if (!itemA.text().isEmpty()) {
                Movie m = new Movie();
                m.setTitle(itemA.text());
                m.setVideoUrl(LineUtil.getLineUtil().swapUrl(itemA.attr("href")));
                m.setCardImageUrl(LineUtil.getLineUtil().swapUrl(item.getElementsByClass("image_imageItem").get(0).attr("src")));
                list.add(m);
            }

        });

        Elements nextPage = document.getElementsByClass(nextPageClass);
        nextUrl = null;
        if (!nextPage.isEmpty()) {
            nextUrl = nextPage.get(0).attr("href");
        }
        return list;
    }

    @Override
    public VideoInfo getVideoDetail(String url) {
        VideoInfo videoInfo = new VideoInfo();
        Document document = RequestUtil.get(url);
        Element infoNode = document.getElementsByClass("introduction_introEllipsis").get(0);
        videoInfo.getDetailInfoList().add(infoNode.text());


        List<LineInfo> lineInfoList = new ArrayList<>();
        Elements playNodes = document.getElementsByClass("drama_catalogTitle");
        Elements playList = document.getElementsByClass("CatalogList_swiperBox");
        for (int i = 0; i < playNodes.size(); i++) {
            Element node = playNodes.get(i);
            List<PlayNumInfo> playItemList = new ArrayList<>();
            Elements playElement = playList.get(i).getElementsByTag("a");
            for (int p = 0; p < playElement.size(); p++) {
                Element item = playElement.get(p);
                PlayNumInfo play = new PlayNumInfo();
                play.setId(p);
                play.setTitle(item.text());
                play.setPageUrl(item.attr("href"));
                playItemList.add(play);
            }

            LineInfo lineInfo = new LineInfo();
            lineInfo.setTitle(node.text());
            lineInfo.setPlayNumInfoList(playItemList);
            lineInfoList.add(lineInfo);
        }

        videoInfo.setLineInfoList(lineInfoList);
        return videoInfo;
    }

    @Override
    public String getPlayUrl(String url) {
        Document document = RequestUtil.get(url);
        String content = document.getElementsByClass("episode_playVideo").get(0).toString();
        return LineUtil.getLineUtil().getPlayUrl(content);
    }

    @Override
    public String getSearchUrl() {
        return LineUtil.getLineUtil().getLine() + "vodsearch/-------------.html?wd=";
    }

    @Override
    public List<Movie> search(String word) {
        String searchUrl = getSearchUrl() + word;
        nextUrl = null;
        return getMovies(searchUrl);
    }

    @Override
    public List<Movie> loadNext() {
        if (nextUrl == null) {
            return Collections.emptyList();
        } else {
            return getMovies(nextUrl);
        }
    }


}
